﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using ExtBusiness.Struct;
using ExtBusiness.FlowOperation;
using ExtBusiness.Common;
using WF_Business;

namespace ExtBusiness.User
{
    /// <summary>
    /// 用户组操作类
    /// 创建人：YZG
    /// 时间：2010-03-02
    /// </summary>
    public class UserGroupHandle
    {
        /// <summary>
        /// 查询用户组
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public static DataTable GetUserGroupById(string userId)
        {
            string sqlNum = "select * from st_user_group where userid='" + userId + "'";
            DataTable dt;
            SysParams.OAConnection().RunSql(sqlNum, out dt);
            return dt;
        }
        /// <summary>
        /// 查询用户所在组名
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public static DataTable GetGroupName(string userId)
        {
            string strSql = " select a.group_name from st_group a where a.groupid in (select gid from st_user_group where userid='" + userId + "')";
            DataTable dt;
            SysParams.OAConnection().RunSql(strSql, out dt);
            return dt;
        }
        /// <summary>
        /// 根据组id删除组
        /// </summary>
        /// <param name="strGroupId">用户组id</param>
        /// <returns></returns>
        public static int DeleteGroupById(string strGroupId)
        {
            string strSql = string.Format("delete from st_group where groupid='{0}'",
                strGroupId);
            return SysParams.OAConnection().RunSql(strSql);
        }

        /// <summary>
        /// 根据Id更新组信息
        /// </summary>
        /// <param name="grpsParam">存放需要更新的组信息</param>
        /// <returns></returns>
        public static int UpdateGroupById(GroupStruct grpsParam)
        {
            string strSql = string.Format("update st_group set GROUP_NAME='{0}' where groupid='{1}'",
                grpsParam.GroupName, grpsParam.GroupId);
            return SysParams.OAConnection().RunSql(strSql);
        }

        /// <summary>
        /// 根据组id获取组信息
        /// </summary>
        /// <param name="strGroupId">用户组id</param>
        /// <returns></returns>
        public static GroupStruct GetGroupById(string strGroupId)
        {
            GroupStruct grpsReturn = new GroupStruct();

            string strSql = string.Format("select GROUP_NAME from st_group where groupid='{0}'",
                strGroupId);

            grpsReturn.GroupId = strGroupId;
            grpsReturn.GroupName = SysParams.OAConnection().GetValue(strSql);

            return grpsReturn;
        }

        /// <summary>
        /// 添加组信息
        /// </summary>
        /// <param name="strGroupName">组名称</param>
        /// <param name="strGroupDescrip">组描述</param>
        /// <returns></returns>
        public static int AddGroup(string strGroupName, string strGroupDescrip)
        {
            string strSql = string.Format("Insert into st_group(GROUPID,GROUP_NAME,MEMO) values('{0}','{1}','{2}')",
                Common.GenerateGuid.NewGuid, strGroupName, strGroupDescrip);
            return SysParams.OAConnection().RunSql(strSql);
        }

        /// <summary>
        /// 获取所有组信息
        /// </summary>
        /// <returns></returns>
        public static List<GroupStruct> GetAllGroupInfo(string groupName)
        {
            string strSql = "select * from st_group ";
            if (!String.IsNullOrEmpty(strSql))
                strSql += " WHERE Group_Name like '%" + groupName + "%'";
            //strSql += " order by INPUTTIME desc";
            DataTable dtPost;
            SysParams.OAConnection().RunSql(strSql, out dtPost);

            List<GroupStruct> lstGroup = new List<GroupStruct>();
            foreach (DataRow drTemp in dtPost.Rows)
            {
                GroupStruct gsTemp = new GroupStruct();
                gsTemp.GroupId = drTemp["GROUPID"].ToString();
                gsTemp.GroupName = drTemp["GROUP_NAME"].ToString();
                lstGroup.Add(gsTemp);
            }

            return lstGroup;
        }

        /// <summary>
        /// 添加组信息
        /// </summary>
        /// <param name="strGroupName">组名称</param>
        /// <param name="strGroupDescrip">组描述</param>
        /// <param name="orderNum">排序字段</param>
        /// <returns></returns>
        public static int AddGroup(string strGroupName, string strGroupDescrip, string orderNum, string isStop)
        {
            string strSql = string.Format("Insert into st_group(GROUPID,GROUP_NAME,MEMO,parent_gid,IS_STOP) values('{0}','{1}','{2}','{3}','{4}')",
                GenerateGuid.NewGuid, strGroupName, strGroupDescrip, orderNum, isStop);
            return SysParams.OAConnection().RunSql(strSql);
        }

        /// <summary>
        /// 根据流程名获取用户信息
        /// </summary>
        /// <param name="strWname">流程名</param>
        /// <param name="strStepName">岗位名称</param>
        /// <returns></returns>
        public static DataTable GetUserInfoByFlowStep(string strWname, string strStepName)
        {
            string strSql = string.Format(@"select distinct e.userid, e.user_name
                  from st_step          a,
                       st_workflow      b,
                       st_group_in_step c,
                       st_user_group    d,
                       st_user          e
                 where a.wid = b.wid
                   and c.wid = b.wid
                   and d.gid = c.gid
                   and d.userid = e.userid
                   and c.stpname = a.ctlid
                   and b.wname='{0}' and a.sname='{1}'", strWname, strStepName);
            DataTable dtReturn = new DataTable();
            SysParams.OAConnection().RunSql(strSql, out dtReturn);
            return dtReturn;
        }

    }
}
